using System;
using System.IO;
using log4net;
using log4net.Config;

namespace VNet.VisiSuite.Common.Logger {
    public class FileLog {
        private const string INSTALL_FOLDER = "\\leanlogic\\VisiSuite 2.0";
        private static volatile FileLog _instance; // Singleton
        private static readonly object _sync_root = new Object();
        private readonly ILog _logger;

        private FileLog() {
            // Make sure that folder for logging data is available.
            string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); // c:\ProgramData 
            path += INSTALL_FOLDER;
            if (!Directory.Exists(path)) {
                Directory.CreateDirectory(path);
            }

            XmlConfigurator.Configure();
            _logger = LogManager.GetLogger("VisiSuite");

            /* TODO allow this to be changed at runtime.
            ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetLoggerRepository()).Root.Level = log4net.Core.Level.Debug;
            ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetLoggerRepository()).RaiseConfigurationChanged(EventArgs.Empty);
             */
        }

        private static FileLog Instance {
            get {
                if (_instance == null) {
                    lock (_sync_root) {
                        if (_instance == null)
                            _instance = new FileLog();
                    }
                }
                return _instance;
            }
        }

        public static void Fatal(string msg) {
            if (Instance._logger.IsFatalEnabled)
                Instance._logger.Fatal(msg);
        }

        public static void Fatal(string msg, Exception ex) {
            if (Instance._logger.IsFatalEnabled)
                Instance._logger.Fatal(msg, ex);
        }

        public static void Error(string msg) {
            if (Instance._logger.IsErrorEnabled)
                Instance._logger.Error(msg);
        }

        public static void Error(string msg, Exception ex) {
            if (Instance._logger.IsErrorEnabled)
                Instance._logger.Error(msg, ex);
        }

        public static void Warn(string msg) {
            if (Instance._logger.IsWarnEnabled)
                Instance._logger.Warn(msg);
        }

        public static void Warn(string msg, Exception ex) {
            if (Instance._logger.IsWarnEnabled)
                Instance._logger.Warn(msg, ex);
        }

        public static void Info(string msg) {
            if (Instance._logger.IsInfoEnabled)
                Instance._logger.Info(msg);
        }

        public static void Info(string msg, Exception ex) {
            if (Instance._logger.IsInfoEnabled)
                Instance._logger.Info(msg, ex);
        }

        public static void Debug(string msg) {
            if (Instance._logger.IsDebugEnabled)
                Instance._logger.Debug(msg);
        }

        public static void Debug(string msg, Exception ex) {
            if (Instance._logger.IsDebugEnabled)
                Instance._logger.Debug(msg, ex);
        }
    }
}